import { CodeListService } from '../app/codelist-service';
import { EntityBaseService, IContext, HttpResponse } from 'ibiz-core';
import { IMAJORENTITY, MAJORENTITY } from '../../entities';
import keys from '../../entities/majorentity/majorentity-keys';
import { MAJORENTITYDTOHelp } from '../../entities/majorentity/majorentitydto-help';


/**
 * 主实体服务对象基类
 *
 * @export
 * @class MAJORENTITYBaseService
 * @extends {EntityBaseService}
 */
export class MAJORENTITYBaseService extends EntityBaseService<IMAJORENTITY> {
    protected get keys(): string[] {
        return keys;
    }
    protected SYSTEMNAME = 'ActiveTYYW';
    protected APPNAME = 'App2';
    protected APPDENAME = 'MAJORENTITY';
    protected APPWFDENAME = 'MAJORENTITY';
    protected APPDENAMEPLURAL = 'MAJORENTITies';
    protected dynaModelFilePath: string = 'PSSYSAPPS/App2/PSAPPDATAENTITIES/MAJORENTITY.json';
    protected APPDEKEY = 'majorentityid';
    protected APPDETEXT = 'majorentityname';
    protected quickSearchFields = ['majorentityname', 'field8', 'field45',];
    protected selectContextParam = {
    };

    constructor(opts?: any) {
        super(opts, 'MAJORENTITY');
    }

    newEntity(data: IMAJORENTITY): MAJORENTITY {
        return new MAJORENTITY(data);
    }

    /**
     * BDXGXCS
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async BDXGXCS(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await this.executeAppDELogic('fontentitylogic', _context, _data);
            return new HttpResponse(_data, {
                ok: true,
                status: 200
            });
        } catch (error) {
            return new HttpResponse({ message: error.message }, {
                ok: false,
                status: 500,
            });
        }
    }
    /**
     * Create
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Create(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await this.beforeExecuteAction(_context, _data, 'Create');
            if (!_data.srffrontuf || _data.srffrontuf != 1) {
                _data[this.APPDEKEY] = null;
            }
            if (_data.srffrontuf != null) {
                delete _data.srffrontuf;
            }
            _data = await MAJORENTITYDTOHelp.get(_context, _data);
            const res = await this.http.post(`/majorentities`, _data);
            res.data = await MAJORENTITYDTOHelp.set(_context, res.data);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * Get
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Get(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            // const res = await this.http.get(`/majorentities/${encodeURIComponent(_context.majorentity)}`, _data);
            // res.data = await MAJORENTITYDTOHelp.set(_context, res.data);
            // res.data = await this.afterExecuteAction(_context, res?.data, 'Get');
            // return res;
            return new HttpResponse(
                {
                    'majorentityid': '1',
                    'majorentityname': "1111",
                    a: 111
                }, {
                ok: true,
                status: 200
            });
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * GetDraft
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async GetDraft(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data[this.APPDENAME?.toLowerCase()] = undefined;
            _data[this.APPDEKEY] = undefined;
            const res = await this.http.get(`/majorentities/getdraft`, _data);
            res.data = await MAJORENTITYDTOHelp.set(_context, res.data);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * Remove
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Remove(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            const res = await this.http.delete(`/majorentities/${encodeURIComponent(_context.majorentity)}`);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * Test
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Test(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await MAJORENTITYDTOHelp.get(_context, _data);
            const res = await this.http.post(`/majorentities/${encodeURIComponent(_context.majorentity)}/test`, _data);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * TestAction
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async TestAction(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await MAJORENTITYDTOHelp.get(_context, _data);
            const res = await this.http.post(`/majorentities/${encodeURIComponent(_context.majorentity)}/testaction`, _data);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * Testfontlogic
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Testfontlogic(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await this.executeAppDELogic('savebeforelogic', _context, _data);
            return new HttpResponse(_data, {
                ok: true,
                status: 200
            });
        } catch (error) {
            return new HttpResponse({ message: error.message }, {
                ok: false,
                status: 500,
            });
        }
    }
    /**
     * Update
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Update(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            _data = await this.beforeExecuteAction(_context, _data, 'Update');
            _data = await MAJORENTITYDTOHelp.get(_context, _data);
            const res = await this.http.put(`/majorentities/${encodeURIComponent(_context.majorentity)}`, _data);
            res.data = await MAJORENTITYDTOHelp.set(_context, res.data);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * FetchDefault
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async FetchDefault(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            // const res = await this.http.get(`/majorentities/fetchdefault`, _data);
            // res.data = await MAJORENTITYDTOHelp.ToDataObjArray(_context,res.data);
            // res.data = await this.afterExecuteActionBatch(_context,res?.data,'FetchDefault');
            // return res;
            return new HttpResponse([
                {
                    'majorentityid': '1',
                    'majorentityname': "1111",
                    a: 2
                },
                {
                    'majorentityid': '2',
                    'majorentityname': "2222",
                    a: 3
                },
                {
                    'majorentityid': '1',
                    'majorentityname': "3333",
                    a: 1
                }
            ], {
                ok: true,
                status: 200
            });
        } catch (error) {
            return this.handleResponseError(error);
        }
    }
    /**
     * Select
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof MAJORENTITYService
     */
    async Select(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
            const res = await this.http.get(`/majorentities/${encodeURIComponent(_context.majorentity)}/select`);
            return res;
        } catch (error) {
            return this.handleResponseError(error);
        }
    }

    /**
     * TestBatch接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MAJORENTITYServiceBase
     */
    public async TestBatch(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        _data = await MAJORENTITYDTOHelp.get(_context, _data);
        const res = await this.http.post(`/majorentities/testbatch`, _data);
        return res;
    }

    /**
     * TestActionBatch接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof MAJORENTITYServiceBase
     */
    public async TestActionBatch(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        _data = await MAJORENTITYDTOHelp.get(_context, _data);
        const res = await this.http.post(`/majorentities/testactionbatch`, _data);
        return res;
    }
}
